<?php



/**
 * This is the model class for table "blog_comment".
 *
 * The followings are the available columns in table 'blog_comment':
 * @property string  $id
 * @property string  $website
 * @property string  $info
 * @property string  $status
 * @property integer $create_date
 * @property string  $user_id
 * @property string  $full_name
 * @property string  $email
 * @property string  $post_id
 * @property string  $post_title
 * @property string  $post_alias
 * @property integer $owner_post
 * @property integer $update_date
 * @property integer $parent_id
 * @property string  $is_read
 * @property string  $is_spam
 * @property string  $logo
 *
 * The followings are the available model relations:
 * @property User    $user
 * @property Post    $post
 */

class Comment extends CActiveRecord {
    /**
     * Returns the static model of the specified AR class.
     *
     * @param string $className active record class name.
     *
     * @return Comment the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'blog_comment';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('info', 'required'),
            array('create_date, owner_post, update_date, parent_id', 'numerical', 'integerOnly' => true),
            array('website, full_name, email, post_title, post_alias, logo', 'length', 'max' => 255),
            array('status, is_read, is_spam', 'length', 'max' => 1),
            array('user_id, post_id', 'length', 'max' => 11),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, website, info, status, create_date, user_id, full_name, email, post_id, post_title, post_alias, owner_post, update_date, parent_id, is_read, is_spam, logo', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'parent'        => array(self::BELONGS_TO, 'Comment', 'parent_id'),
            'children'      => array(self::HAS_MANY, 'Comment', 'parent_id', 'order' => 'id desc'),
            'user' => array(self::BELONGS_TO, 'User', 'user_id'),
            'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id'          => 'ID',
            'website'     => 'Website',
            'info'        => 'Info',
            'status'      => 'Status',
            'create_date' => 'Create Date',
            'user_id'     => 'User',
            'full_name'   => 'Full Name',
            'email'       => 'Email',
            'post_id'     => 'Post',
            'post_title'  => 'Post Title',
            'post_alias'  => 'Post Alias',
            'owner_post'  => 'Owner Post',
            'update_date' => 'Update Date',
            'parent_id'   => 'Parent',
            'is_read'     => 'Is Read',
            'is_spam'     => 'Is Spam',
            'logo'        => 'Logo',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id, true);
        $criteria->compare('website', $this->website, true);
        $criteria->compare('info', $this->info, true);
        $criteria->compare('status', $this->status, true);
        $criteria->compare('create_date', $this->create_date);
        $criteria->compare('user_id', $this->user_id, true);
        $criteria->compare('full_name', $this->full_name, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('post_id', $this->post_id, true);
        $criteria->compare('post_title', $this->post_title, true);
        $criteria->compare('post_alias', $this->post_alias, true);
        $criteria->compare('owner_post', $this->owner_post);
        $criteria->compare('update_date', $this->update_date);
        $criteria->compare('parent_id', $this->parent_id);
        $criteria->compare('is_read', $this->is_read, true);
        $criteria->compare('is_spam', $this->is_spam, true);
        $criteria->compare('logo', $this->logo, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function show_comments($parent_id = 0, $post) {
        $cacheComment = 'cache_' . $post->alias . '_comments_' . $parent_id;
        $comments     = Yii::app()->cache->get($cacheComment);
        if (!$comments) {
            $criteria = self::criteriaModel($parent_id, $post->alias);
            $comments = self::model()->findAll($criteria);
            Yii::app()->cache->set($cacheComment, $comments, TenMinutes);
        }
        if (count($comments)) {
            foreach ($comments as $comment) {
                $imageavatar = !empty($comment->logo) ? Helper::renderImage($comment->logo, 'uploads/avatar', ',', true, true) : '/uploads/avatar.jpg';
                $username = !empty($comment->user_id) ? $comment->user->username : $comment->full_name;
                ?>
                <div class="comment first <?php if ($comment->parent == $parent_id) { ?>odd<?php } else { ?>re pair<?php } ?>" id="comment_<?php echo $comment->id; ?>">
                    <div class="row-fluid">
                        <div class="span2 avatar imgLiquidFill imgLiquid-avatar">
                            <img data-original="<?php echo $imageavatar; ?>" class="avatar" src="<?php echo $imageavatar; ?>" alt="<?php echo $username; ?>" />
                        </div>
                        <div class="span10 clearfix">
                            <p class="reply pull-right">on <?php echo Helper::getTimeAgo($comment->create_date); ?> /
                                <a href="javascript:void(0);" id="comment_<?php echo $comment->id; ?>" class="replyComment" title="Reply">Reply</a></p>
                            <p class="by">By <span class="accent"><?php echo $username; ?></span></p>
                            <p><?php echo nl2br($comment->info); ?></p>
                        </div>
                    </div>
                    <?php if ($comment->children) { ?>
                        <?php Comment::show_comments($comment->id, $post); ?>
                    <?php } ?>
                </div>
            <?php
            }
        } else {
            echo '<p>' . Helper::t('No_Results_key') . '</p>';
        }
    }

    public static function criteriaModel($parentId=0, $postAlias='', $limit='') {
        $criteria = new CDbCriteria();

        $criteria->compare('status', APPROVED);
        $criteria->compare('is_spam', NONE_SPAM);
        $criteria->compare('parent_id', $parentId);
        $criteria->compare('post_alias', $postAlias);

        if(!empty($limit) && is_int($limit)) {
            $criteria->limit = $limit;
        }

        return $criteria;
    }

    protected function beforeSave() {
        if ($this->isNewRecord) {
            if (Helper::get('postAlias')) {
                $pAlias = Helper::get('postAlias');
                $post   = CreateCache::usingCache('Post', false, $pAlias);
                $this->post_id = $post->id;
            }
            $this->create_date = $this->update_date = time();
            $this->status      = PENDING;
            $this->is_spam     = NONE_SPAM;
            $this->owner_post = $post->owner->id;
        } else {
            $this->update_date = time();
        }

        return parent::beforeSave();
    }

    protected function afterSave() {
        if ($this->isNewRecord) {
            $post = Post::model()->find('id=:id', array(':id' => $this->post_id));
            $this->updateByPk($this->id, array(
                'post_title' => $post->title,
                'post_alias' => Helper::unicode_convert($post->title),
                'logo'       => $this->user_id ? $post->user->id : ''
            ));
        }

        return parent::afterSave();
    }
}